package org.cangku.controller;


import org.cangku.dao.staffDao;
import org.cangku.entity.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/worker")
public class staffcontrollers {
    @Autowired
    private staffDao staffDao;


    /*模糊分页查询*/
    @RequestMapping("/staffer")
    @ResponseBody
    public Map<String,Object> query(String name,int offset,int limit){
        Map<String,Object> map = new HashMap<>();
        map.put("rows",staffDao.getAllStaff(name,offset,limit));
        map.put("total",staffDao.getTotalCount(name));
        return map;
    }
    //根据id获取信息
    @RequestMapping("/selectByid")
    @ResponseBody
    public List<Staff> selectById(int yid){
        return staffDao.selectById(yid);
    }

    //单个删除
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(int yid){
        boolean is = staffDao.delInfo(yid);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("rows",staffDao.getAllStaff("",0,4));
        map.put("total",staffDao.getTotalCount(""));
        return map;
    }
    //批量删除
    @RequestMapping("/del")
    @ResponseBody
    public Map<String,Object> del(String ids){
        System.out.println(ids);
        String[] ss = ids.split("-");
        for(int i=0;i<ss.length;i++){
            staffDao.del(Integer.parseInt(ss[i]));
            System.out.println(ss[i]);
        }
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",staffDao.getAllStaff("",0,4));
        map.put("total",staffDao.getTotalCount(""));
        return map;
    }
    //添加员工
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> addwork(Staff staff) {
        staff.setyId(0);
        if(staff.getyId()==0){
             boolean is = staffDao.insert(staff);
        }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("rows", staffDao.getAllStaff("", 0, 4));
            map.put("total", staffDao.getTotalCount(""));
            return map;

    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> editWork(Staff staff){
        boolean is = staffDao.update(staff);
        System.out.println(is);
        Map<String,Object> map = new HashMap<String,Object>();
       map.put("rows", staffDao.getAllStaff("", 0, 4));
        map.put("total", staffDao.getTotalCount(""));
        return  map;
    }

    @RequestMapping("/query")
    @ResponseBody
    public List<Staff> query(){
        return staffDao.query();
    }
}
